package com.leyou.page.service;

import com.leyou.item.pojo.Category;
import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.pojo.Spu;
import com.leyou.page.client.BrandClient;
import com.leyou.page.client.CategoryClient;
import com.leyou.page.client.GoodsClient;
import com.leyou.page.client.SpecificationClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author: taft
 * @Date: 2018-10-13 18:02
 */
@Service
public class GoodsService {

    @Autowired
    private GoodsClient goodsClient;


    @Autowired
    private BrandClient brandClient;

    @Autowired
    private CategoryClient categoryClient;


    @Autowired
    private SpecificationClient specificationClient;

    public Map<String,Object> loadModel(Long id){
        Map<String,Object> modelMap = new HashMap<>();

        //封装spu
        Spu spu = goodsClient.querySpuById(id);
        modelMap.put("spu",spu);

        //封装skus，根据spuid查询对应的所有的sku
        modelMap.put("skus",goodsClient.querySkuBySpuId(id));

        //封装spuDetail
        modelMap.put("spuDetail",goodsClient.querySpuDetailById(id));



        modelMap.put("brand",brandClient.queryBrandByIds(Arrays.asList(spu.getBrandId())).get(0));


        List<String> names = categoryClient.queryNameByIds(Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3()));

        List<Category> categories = new ArrayList<>();

        Category category1 = new Category();
        category1.setId(spu.getCid1());
        category1.setName(names.get(0));

        categories.add(category1);

        Category category2 = new Category();
        category2.setId(spu.getCid2());
        category2.setName(names.get(1));

        categories.add(category2);


        Category category3 = new Category();
        category3.setId(spu.getCid3());
        category3.setName(names.get(2));

        categories.add(category3);

        modelMap.put("categories",categories);


        // 查询规格组及组内参数,这个查询完成后只有属性没有值
        List<SpecGroup> groups = this.specificationClient.querySpecsByCid(spu.getCid3());

        modelMap.put("groups",groups);


        // 查询商品分类下的特有规格参数
        List<SpecParam> params =
                this.specificationClient.querySpecParam(null, spu.getCid3(), null, false);

        // 处理成id:name格式的键值对
        Map<Long,String> paramMap = new HashMap<>();
        for (SpecParam param : params) {
            paramMap.put(param.getId(),param.getName());
        }

        modelMap.put("paramMap", paramMap);



        return modelMap;
    }
}
